package com.whims.MemoryError.OutOfMemoryError;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * vm options:-XX:MetaspaceSize=1M -XX:MaxMetaspaceSize=10M
 * 方法区内存溢出测试，运用CGLib操作字节码生成大量的类
 */
public class JavaMethodAreaOOM {

    static class OOMObject{}


    public static void main(String []args){
        while (true){
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invokeSuper(o,objects);
                }
            });
            enhancer.create();
        }
    }



}
